package com.zhejiangzhengyuan.municipal_common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhejiangzhengyuan.municipal_common.entity.PumpEstimatedValue;
import com.zhejiangzhengyuan.municipal_common.entity.PumpStation;
import com.zhejiangzhengyuan.municipal_common.entity.PumpStationDeviceMapping;
import com.zhejiangzhengyuan.municipal_common.entity.vo.PumpMonitoreVo;
import com.zhejiangzhengyuan.municipal_common.mapper.PumpStationDeviceMappingMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.PumpStationMapper;
import com.zhejiangzhengyuan.municipal_common.service.PumpEstimatedValueService;
import com.zhejiangzhengyuan.municipal_common.service.PumpStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author linju
 * @since 2019-12-14
 */
@Service
public class PumpStationServiceImpl extends ServiceImpl<PumpStationMapper, PumpStation> implements PumpStationService {

    private String[] pumpCode={"3705020103010000009","3705020103010000008","3705020103010000007","3705020103010000006"};
    @Autowired private PumpStationMapper pumpStationMapper;
    @Autowired private PumpStationDeviceMappingMapper pumpStationDeviceMappingMapper;
    @Autowired private PumpEstimatedValueService pumpEstimatedValueService;
    @Autowired private MongoTemplate mongoTemplate;

    //实时监控泵站的数据
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PumpMonitoreVo> getPumpMessage() {
        List<PumpStationDeviceMapping> pumpStationDeviceMappings = pumpStationDeviceMappingMapper.selectList(null);
        //作为返回的参数值
        List<PumpMonitoreVo> list1=new ArrayList<>();
        QueryWrapper<PumpStation> queryWrapper=new QueryWrapper<PumpStation>();
        queryWrapper.eq("code",pumpCode[0]);
        PumpStation pumpStation = pumpStationMapper.selectOne(queryWrapper);
        //查询舞阳泵站的控制模式及状态
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("TAG_NAME").is("雨水泵控制模式");
        criteria.and("STATION_KEY").is(pumpCode[0]);
        query.addCriteria(criteria);
        PumpEstimatedValue pumpEstimatedValue = mongoTemplate.findOne(query, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        //返回实体类
        PumpMonitoreVo pumpMonitoreVo=new PumpMonitoreVo();
        pumpMonitoreVo.setStationKey(pumpCode[0]);
        pumpMonitoreVo.setPumpStation("舞阳泵站");
        pumpMonitoreVo.setLongitude(pumpStation.getLongitude());
        pumpMonitoreVo.setLatitude(pumpStation.getLatitude());
        pumpMonitoreVo.setDate(pumpEstimatedValue.getDate());
        //舞阳设置控制模式
        SelectPumpStatus(pumpEstimatedValue,pumpMonitoreVo);
        Query query2 = new Query();
        Criteria criteria2 = new Criteria();
        criteria2.and("STATION_KEY").is(pumpCode[0]);
        query2.addCriteria(criteria2);
        query2.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "故障信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues2 = mongoTemplate.find(query2, PumpEstimatedValue.class);
        for (PumpEstimatedValue estimatedValue : pumpEstimatedValues2) {
            if (!estimatedValue.getValue().equals("0")){
                pumpMonitoreVo.setStatus("故障");
                break;
            }
        }
        //舞阳运行信号
        Query query3 = new Query();
        Criteria criteria3 = new Criteria();
        criteria3.and("STATION_KEY").is(pumpCode[0]);
        query3.addCriteria(criteria3);
        query3.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "运行信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues3 = mongoTemplate.find(query3, PumpEstimatedValue.class);
        SelectPumpControll(pumpMonitoreVo, pumpEstimatedValues3);

        //舞阳雨水池液位
        Query query4 = new Query();
        Criteria criteria4 = new Criteria();
        criteria4.and("TAG_DESC").is("雨水池液位");
        criteria4.and("STATION_KEY").is(pumpCode[0]);
        query4.addCriteria(criteria4);
        PumpEstimatedValue pumpEstimatedValue1 = mongoTemplate.findOne(query4, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        pumpMonitoreVo.setMLit101(pumpEstimatedValue1.getValue());
        //舞阳污水池液位
        Query query5 = new Query();
        Criteria criteria5 = new Criteria();
        criteria5.and("TAG_DESC").is("污水池液位");
        criteria5.and("STATION_KEY").is(pumpCode[0]);
        query5.addCriteria(criteria5);
        PumpEstimatedValue pumpEstimatedValue2 = mongoTemplate.findOne(query5, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        pumpMonitoreVo.setMLit102(pumpEstimatedValue2.getValue());
        pumpMonitoreVo.setList(pumpStationDeviceMappings);
        pumpMonitoreVo.setWid(pumpStationMapper.GetWarnId(pumpCode[0]));
        list1.add(pumpMonitoreVo);

        //304泵站
        QueryWrapper<PumpStation> queryWrapper1=new QueryWrapper<PumpStation>();
        queryWrapper1.eq("code",pumpCode[1]);
        PumpStation pumpStation1 = pumpStationMapper.selectOne(queryWrapper1);
        //304泵站控制模式
        Query query1 = new Query();
        Criteria criteria1 = new Criteria();
        criteria1.and("TAG_NAME").is("泵控制模式");
        criteria1.and("STATION_KEY").is(pumpCode[1]);
        query1.addCriteria(criteria1);
        PumpEstimatedValue pumpEstimatedValue11 = mongoTemplate.findOne(query1, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");

        PumpMonitoreVo pumpMonitoreVo1=new PumpMonitoreVo();
        pumpMonitoreVo1.setStationKey(pumpCode[1]);
        pumpMonitoreVo1.setPumpStation("304泵站");
        pumpMonitoreVo1.setLongitude(pumpStation1.getLongitude());
        pumpMonitoreVo1.setLatitude(pumpStation1.getLatitude());
        pumpMonitoreVo1.setDate(pumpEstimatedValue.getDate());
        SelectPumpStatus(pumpEstimatedValue11, pumpMonitoreVo1);
        //304泵站故障信号
        Query query6 = new Query();
        Criteria criteria6 = new Criteria();
        criteria6.and("STATION_KEY").is(pumpCode[1]);
        query6.addCriteria(criteria6);
        query6.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "故障信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues13 = mongoTemplate.find(query6, PumpEstimatedValue.class);
        for (PumpEstimatedValue estimatedValue : pumpEstimatedValues13) {
            if (!estimatedValue.getValue().equals("0")){
                pumpMonitoreVo1.setStatus("故障");
                break;
            }
        }
        //304泵站运行信号
        Query query7 = new Query();
        Criteria criteria7 = new Criteria();
        criteria7.and("STATION_KEY").is(pumpCode[1]);
        query7.addCriteria(criteria7);
        query7.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "运行信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues14 = mongoTemplate.find(query7, PumpEstimatedValue.class);
        SelectPumpControll(pumpMonitoreVo1, pumpEstimatedValues14);

        //304泵站液位
        Query query8= new Query();
        Criteria criteria8 = new Criteria();
        criteria8.and("TAG_DESC").is("液位");
        criteria8.and("STATION_KEY").is(pumpCode[1]);
        query8.addCriteria(criteria8);
        PumpEstimatedValue pumpEstimatedValue14 = mongoTemplate.findOne(query8, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        pumpMonitoreVo1.setValue(pumpEstimatedValue14.getValue());

        //304泵站硫化氢
        Query query9= new Query();
        Criteria criteria9 = new Criteria();
        criteria9.and("TAG_DESC").is("硫化氢");
        criteria9.and("STATION_KEY").is(pumpCode[1]);
        query9.addCriteria(criteria9);
        PumpEstimatedValue pumpEstimatedValue15 = mongoTemplate.findOne(query9, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        pumpMonitoreVo1.setH2S(pumpEstimatedValue15.getValue());

        //304泵站氨气
        Query query10= new Query();
        Criteria criteria10 = new Criteria();
        criteria10.and("TAG_DESC").is("氨气");
        criteria10.and("STATION_KEY").is(pumpCode[1]);
        query10.addCriteria(criteria10);
        PumpEstimatedValue pumpEstimatedValue16 = mongoTemplate.findOne(query10, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        pumpMonitoreVo1.setNH3(pumpEstimatedValue16.getValue());
        pumpMonitoreVo1.setList(pumpStationDeviceMappings);
        pumpMonitoreVo1.setWid(pumpStationMapper.GetWarnId(pumpCode[1]));
        list1.add(pumpMonitoreVo1);


        //春晖泵站
        QueryWrapper<PumpStation> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("code",pumpCode[2]);
        PumpStation pumpStation2 = pumpStationMapper.selectOne(queryWrapper2);
        //春晖泵站泵控制模式
        Query query11 = new Query();
        Criteria criteria11 = new Criteria();
        criteria11.and("TAG_DESC").is("泵控制模式");
        criteria11.and("STATION_KEY").is(pumpCode[2]);
        query11.addCriteria(criteria11);
        PumpEstimatedValue pumpEstimatedValue21 = mongoTemplate.findOne(query11, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        PumpMonitoreVo pumpMonitoreVo2=new PumpMonitoreVo();
        pumpMonitoreVo2.setStationKey(pumpCode[2]);
        pumpMonitoreVo2.setPumpStation("春晖泵站");
        pumpMonitoreVo2.setLongitude(pumpStation2.getLongitude());
        pumpMonitoreVo2.setLatitude(pumpStation2.getLatitude());
        pumpMonitoreVo2.setDate(pumpEstimatedValue21.getDate());
        SelectPumpStatus(pumpEstimatedValue21, pumpMonitoreVo2);

        //春晖泵站故障信号
        Query query12 = new Query();
        Criteria criteria12 = new Criteria();
        criteria12.and("STATION_KEY").is(pumpCode[2]);
        query12.addCriteria(criteria12);
        query12.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "故障信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues23 = mongoTemplate.find(query12, PumpEstimatedValue.class);
        for (PumpEstimatedValue estimatedValue : pumpEstimatedValues23) {
            if (!estimatedValue.getValue().equals("0")){
                pumpMonitoreVo2.setStatus("故障");
                break;
            }
        }
        //春晖泵站运行信号
        Query query13 = new Query();
        Criteria criteria13 = new Criteria();
        criteria13.and("STATION_KEY").is(pumpCode[2]);
        query13.addCriteria(criteria13);
        query13.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "运行信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues24 = mongoTemplate.find(query13, PumpEstimatedValue.class);
        SelectPumpControll(pumpMonitoreVo2, pumpEstimatedValues24);

        //春晖泵站液位
        Query query14= new Query();
        Criteria criteria14 = new Criteria();
        criteria14.and("TAG_DESC").is("液位");
        criteria14.and("STATION_KEY").is(pumpCode[2]);
        query14.addCriteria(criteria8);
        PumpEstimatedValue pumpEstimatedValue17 = mongoTemplate.findOne(query14, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        pumpMonitoreVo2.setValue(pumpEstimatedValue17.getValue());

        //春晖泵站硫化氢
        Query query15= new Query();
        Criteria criteria15 = new Criteria();
        criteria15.and("TAG_DESC").is("硫化氢");
        criteria15.and("STATION_KEY").is(pumpCode[2]);
        query15.addCriteria(criteria15);
        PumpEstimatedValue pumpEstimatedValue18 = mongoTemplate.findOne(query15, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        pumpMonitoreVo2.setH2S(pumpEstimatedValue18.getValue());

        //春晖泵站氨气
        Query query16= new Query();
        Criteria criteria16 = new Criteria();
        criteria16.and("TAG_DESC").is("氨气");
        criteria16.and("STATION_KEY").is(pumpCode[2]);
        query16.addCriteria(criteria16);
        PumpEstimatedValue pumpEstimatedValue19 = mongoTemplate.findOne(query16, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        pumpMonitoreVo2.setNH3(pumpEstimatedValue19.getValue());
        pumpMonitoreVo2.setList(pumpStationDeviceMappings);
        pumpMonitoreVo2.setWid(pumpStationMapper.GetWarnId(pumpCode[2]));
        list1.add(pumpMonitoreVo2);

        //振兴泵站
        QueryWrapper<PumpStation> queryWrapper4=new QueryWrapper<>();
        queryWrapper4.eq("code",pumpCode[3]);
        PumpStation pumpStation4 = pumpStationMapper.selectOne(queryWrapper4);
        //振兴泵控制模式
        Query query17 = new Query();
        Criteria criteria17 = new Criteria();
        criteria17.and("TAG_NAME").is("泵控制模式");
        query17.addCriteria(criteria17);
        PumpEstimatedValue pumpEstimatedValue31 = mongoTemplate.findOne(query17, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        PumpMonitoreVo pumpMonitoreVo3=new PumpMonitoreVo();
        pumpMonitoreVo3.setStationKey(pumpCode[3]);
        pumpMonitoreVo3.setPumpStation("振兴泵站");
        pumpMonitoreVo3.setLongitude(pumpStation4.getLongitude());
        pumpMonitoreVo3.setLatitude(pumpStation4.getLatitude());
        pumpMonitoreVo3.setDate(pumpEstimatedValue31.getDate());
        SelectPumpStatus(pumpEstimatedValue31, pumpMonitoreVo3);

        //振兴泵故障信号
        Query query18 = new Query();
        Criteria criteria18 = new Criteria();
        criteria10.and("STATION_KEY").is(pumpCode[3]);
        query18.addCriteria(criteria18);
        query18.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "故障信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues33 = mongoTemplate.find(query18, PumpEstimatedValue.class);
        for (PumpEstimatedValue estimatedValue : pumpEstimatedValues33) {
            if (!estimatedValue.getValue().equals("0")){
                pumpMonitoreVo3.setStatus("故障");
                break;
            }
        }

        //振兴泵运行信号
        Query query19 = new Query();
        Criteria criteria19 = new Criteria();
        criteria19.and("STATION_KEY").is(pumpCode[3]);
        query19.addCriteria(criteria19);
        query19.addCriteria(Criteria.where("TAG_NAME").regex(".*?\\" + "运行信号" + ".*"));
        List<PumpEstimatedValue> pumpEstimatedValues34 = mongoTemplate.find(query19, PumpEstimatedValue.class);
        SelectPumpControll(pumpMonitoreVo3, pumpEstimatedValues34);

        //振兴泵液位
        Query query20= new Query();
        Criteria criteria20 = new Criteria();
        criteria20.and("TAG_DESC").is("液位");
        criteria20.and("STATION_KEY").is(pumpCode[3]);
        query20.addCriteria(criteria20);
        PumpEstimatedValue pumpEstimatedValue20 = mongoTemplate.findOne(query20, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        pumpMonitoreVo3.setValue(pumpEstimatedValue20.getValue());

        //振兴泵桥下液位
        Query query21= new Query();
        Criteria criteria21 = new Criteria();
        criteria21.and("TAG_DESC").is("桥下液位");
        criteria21.and("STATION_KEY").is(pumpCode[3]);
        query21.addCriteria(criteria21);
        PumpEstimatedValue pumpEstimatedValue22 = mongoTemplate.findOne(query21, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        pumpMonitoreVo3.setBridge(pumpEstimatedValue22.getValue());

        //振兴泵硫化氢
        Query query22= new Query();
        Criteria criteria22 = new Criteria();
        criteria22.and("TAG_DESC").is("硫化氢");
        criteria22.and("STATION_KEY").is(pumpCode[3]);
        query22.addCriteria(criteria22);
        PumpEstimatedValue pumpEstimatedValue23 = mongoTemplate.findOne(query22, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        pumpMonitoreVo3.setH2S(pumpEstimatedValue23.getValue());

        //振兴泵氨气
        Query query23= new Query();
        Criteria criteria23 = new Criteria();
        criteria23.and("TAG_DESC").is("氨气");
        criteria23.and("STATION_KEY").is(pumpCode[3]);
        query23.addCriteria(criteria23);
        PumpEstimatedValue pumpEstimatedValue24 = mongoTemplate.findOne(query23, PumpEstimatedValue.class,"ROBIN_HWSERVICE_CONFIG");
        pumpMonitoreVo3.setNH3(pumpEstimatedValue24.getValue());
        pumpMonitoreVo3.setList(pumpStationDeviceMappings);
        pumpMonitoreVo3.setWid(pumpStationMapper.GetWarnId(pumpCode[3]));
        list1.add(pumpMonitoreVo3);
        return list1;
    }

    //泵站雨污水处理终端
    @Override
    public List<PumpStation> getPumpLiquidMessage() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.notLike("name","泵站");
        List<PumpStation> pumpStations = pumpStationMapper.selectList(queryWrapper);
        return pumpStations;
    }

    //根据Id设置报警阈值
    @Override
    public Integer updatePumpLiquidById(Long id,String value) {
        PumpStationDeviceMapping pumpStationDeviceMapping = pumpStationDeviceMappingMapper.selectById(id);
        pumpStationDeviceMapping.setValue(value);
        int i = pumpStationDeviceMappingMapper.updateById(pumpStationDeviceMapping);
        return i;
    }


    private void SelectPumpStatus(PumpEstimatedValue pumpEstimatedValue31, PumpMonitoreVo pumpMonitoreVo) {
        if (pumpEstimatedValue31==null){
            pumpMonitoreVo.setControl("手动");
        }else {
            if (pumpEstimatedValue31.getValue().equals("1")){
                pumpMonitoreVo.setControl("自动");
            }if (pumpEstimatedValue31.getValue().equals("0")){
                pumpMonitoreVo.setControl("远控");
            }
        }
    }

    private void SelectPumpControll(PumpMonitoreVo pumpMonitoreVo, List<PumpEstimatedValue> pumpEstimatedValues34) {
        for (PumpEstimatedValue estimatedValue : pumpEstimatedValues34) {
            if (estimatedValue.getValue().equals("0")){
                pumpMonitoreVo.setStatus("停止");
            }if (estimatedValue.getValue().equals("1")){
                pumpMonitoreVo.setStatus("运行");
            }
        }
    }
}
